<?php
namespace app\user\logic\user;

use app\user\model\user\SystemNotify as SystemNotifyModel;
use app\admin\logic\system\Config as SystemConfig;

/**
 * 消息通知
 */
class SystemNotify extends SystemNotifyModel
{

	/**
	 * 用户未读信息
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date：   2020-05-08
	 * @return [type]                      [description]
	 */
	public function unread()
	{
		return $this->scope('unread')->select();
	}

	/**
	 * 用户未读信息统计
	 * Author 亓官雨树<lucky.max@foxmail.com>
	 * Date：   2020-05-08
	 * @return [type]                      [description]
	 */
	public function unreadCount($type = 0)
	{
		return request()->mid ? $this->scope('unread', $type)->count() : 0;
	}

	// 
	public function getCourseNotify($status)
	{
		return $this
			->with(['user.avatar_url'])
			->courseList($status)
			->order('create_time', 'DESC')
			// ->buildSql();
			->paginate($this->getListRows());
	}

	public function getSystemNotify($status)
	{
		return $this
			->with(['user.avatar_url'])
			->systemList($status)
			->order('create_time', 'DESC')
			->paginate($this->getListRows());
	}

	// 设置已读
	public function setRead($id)
	{
		$data = $this->findOrEmpty($id);
		if ( !$data->isEmpty() ) {
			$data->is_read = 1;
			$data->save();
		}
	}

	public function setAllRead($notify_type)
	{
		$list = $this->scope('unread', $notify_type)->select();
		
		if ( !$list->isEmpty() ) $list->update(['is_read'=>1]);
	}
}
